<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
     <!-- <el-form-item label="摘要" prop="zhaiyao">
        <el-input
          v-model="queryParams.zhaiyao"
          placeholder="请输入摘要"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="业务类型" prop="ywType">
        <el-select v-model="queryParams.ywType" placeholder="请选择业务类型" clearable>
          <el-option
            v-for="dict in dict.type.sk_yw_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="业务单号" prop="ywId">
        <el-input
          v-model="queryParams.ywId"
          placeholder="请输入业务单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单号" prop="ywId2">
        <el-input
          v-model="queryParams.ywId2"
          placeholder="请输入订单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="cusId">
        <el-select  v-model="queryParams.cusId" clearable filterable placeholder="请选择客户">
          <el-option
            v-for="item in khList"
            :key="item.id"
            :label="item.cusName"
            :value="parseInt(item.id)">
          </el-option>
        </el-select>
      </el-form-item>
     <!-- <el-form-item label="银行账户" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入银行账户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="收款方式" prop="skfs">
        <el-select v-model="queryParams.skfs" placeholder="请选择收款方式" clearable>
          <el-option
            v-for="dict in dict.type.sz_fs"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="收款金额" prop="skMoney">
        <el-input
          v-model="queryParams.skMoney"
          placeholder="请输入收款金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优惠金额" prop="yhMoney">
        <el-input
          v-model="queryParams.yhMoney"
          placeholder="请输入优惠金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="应收金额" prop="ysMoney">
        <el-input
          v-model="queryParams.ysMoney"
          placeholder="请输入应收金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
        <el-form-item label="收款时间" prop="skDate">
          <el-date-picker
            v-model="skDate"
            type="daterange"
            @change="handleZfDate"
            unlink-panels
            value-format="yyyy-MM-dd"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jiesuan:shoukuan:add']"
        >新增</el-button>
      </el-col>
    <!--  <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jiesuan:shoukuan:edit']"
        >修改</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jiesuan:shoukuan:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jiesuan:shoukuan:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shoukuanList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="业务类型" align="center" prop="ywType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sk_yw_type" :value="scope.row.ywType"/>
        </template>
      </el-table-column>
      <el-table-column label="业务单号" width="120" align="center" prop="ywId" />
      <el-table-column label="订单号" width="120" align="center" prop="ywId2" />
      <el-table-column label="客户名称" align="center" prop="cusName" show-overflow-tooltip />
      <el-table-column label="收款方式" align="center" prop="skfs">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sz_fs" :value="scope.row.skfs"/>
        </template>
      </el-table-column>
      <el-table-column label="银行账户" show-overflow-tooltip align="center" prop="account" width="130"  />
      <el-table-column label="应收金额" align="center" prop="ysMoney" />
      <el-table-column label="优惠金额" align="center" prop="yhMoney" />
      <el-table-column label="收款金额" align="center" prop="skMoney" />
      <el-table-column label="收款时间" align="center" prop="skDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.skDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="摘要" width="120" show-overflow-tooltip align="center" prop="zhaiyao" />
      <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
         <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jiesuan:shoukuan:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jiesuan:shoukuan:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改收款记账对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
       <el-row>
         <el-col :span="24">
           <el-form-item label="摘要" prop="zhaiyao">
             <el-input v-model="form.zhaiyao" placeholder="请输入摘要" />
           </el-form-item>
         </el-col>
       </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="业务类型" prop="ywType">
          <el-select clearable @change="handleYeType" v-model="form.ywType" placeholder="请选择业务类型">
            <el-option
              v-for="dict in dict.type.sk_yw_type"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="业务单号" prop="ywId">
          <el-select filterable @change="handleGetOrder" v-model="form.ywId" placeholder="请选择业务单号" clearable>
            <el-option
              v-for="item in ywIdOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="订单号" prop="ywId2">
          <el-input readonly v-model="form.ywId2" placeholder="自动获取无需输入" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="客户名称" prop="cusId">
            <el-select @change="handleCusChange" v-model="form.cusId" clearable filterable placeholder="请选择客户">
              <el-option
                v-for="item in khList"
                :key="item.id"
                :label="item.cusName"
                :value="parseInt(item.id)">
              </el-option>
            </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="银行账户" prop="account">
          <el-input v-model="form.account" placeholder="请输入银行账户" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
          <el-form-item label="收款方式" prop="skfs">
          <el-select v-model="form.skfs" placeholder="请选择收款方式">
            <el-option
              v-for="dict in dict.type.sz_fs"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
          <el-form-item label="收款时间" prop="skDate">
          <el-date-picker clearable
                          v-model="form.skDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择收款时间">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="应收金额" prop="ysMoney">
              <el-input-number readonly v-model="form.ysMoney" :precision="2" controls-position="right" placeholder="应收金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
        <el-form-item label="优惠金额" prop="yhMoney">
          <el-input-number @blur="handleMonty" v-model="form.yhMoney" :precision="2" controls-position="right" placeholder="请输入优惠金额" />
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="收款金额" prop="skMoney">
              <el-input-number  :precision="2" v-model="form.skMoney" controls-position="right"  placeholder="请输入收款金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShoukuan, getShoukuan, delShoukuan, addShoukuan, updateShoukuan,getSurplusShoukuan} from "@/api/jiesuan/shoukuan";
import { getChukumainBychukuFlag } from "@/api/business/chukumain";
import { listJcCusInfo } from "@/api/base/jcCusInfo";

export default {
  name: "Shoukuan",
  dicts: ['sk_yw_type', 'sz_fs'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 收款记账表格数据
      shoukuanList: [],
      ywIdOptions: [],
      skDate: [],
      // 客户列表
      khList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zhaiyao: null,
        ywType: null,
        ywId: null,
        ywId2: null,
        cusName: null,
        cusId: null,
        account: null,
        skfs: null,
        skMoney: null,
        yhMoney: null,
        ysMoney: null,
        skDate: null,
        querySkDate1: null,
        querySkDate2: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        ywType: [
          { required: true, message: "业务类型不能为空", trigger: "change" }
        ],
        zhaiyao: [
          { required: true, message: "摘要不能为空", trigger: "change" }
        ],
        skDate: [
          { required: true, message: "收款日期不能为空", trigger: "change" }
        ],
        /*    cusName: [
              { required: true, message: "客户名称不能为空", trigger: "blur" }
            ],*/
        /* account: [
           { required: true, message: "银行账户不能为空", trigger: "blur" }
         ],*/
        skfs: [
          { required: true, message: "收款方式不能为空", trigger: "change" }
        ],
        skMoney: [
          { required: true, message: "收款金额不能为空", trigger: "blur" },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的格式,可保留两位小数' }
        ],
        yhMoney: [
          { required: true, message: "优惠金额不能为空", trigger: "blur" },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的格式,可保留两位小数' }
        ],
        ysMoney: [
          { required: true, message: "应收金额不能为空", trigger: "blur" },
          { pattern: /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/, message: '请输入正确的格式,可保留两位小数' }
        ],
      }
    };
  },
  created() {
    this.getList();
    listJcCusInfo(this.queryParams2).then(response => {
      this.khList = response.rows;
    });
  },
  methods: {
    /** 查询收款记账列表 */
    getList() {
      this.loading = true;
      listShoukuan(this.queryParams).then(response => {
        this.shoukuanList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        zhaiyao: null,
        ywType: null,
        ywId: null,
        ywId2: null,
        cusName: null,
        account: null,
        skfs: null,
        skMoney: 0,
        yhMoney: 0,
        ysMoney: 0,
        skDate:new Date(),
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.skDate=[];
      this.queryParams.querySkDate1=null;
      this.queryParams.querySkDate2=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleZfDate(){
      this.queryParams.querySkDate1=this.skDate[0]
      this.queryParams.querySkDate2=this.skDate[1]
    },
    handleYeType(val){
      if(val=='订单出库' || val=='成品出库'){
        this.form.ywId=null;
        this.form.cusName = null;
        this.form.cusId = null;
        this.form.ywId2 = null;
        this.form.skMoney = 0;
        this.form.yhMoney = 0;
        this.form.ysMoney = 0;
        getChukumainBychukuFlag('2').then(response => {
          this.ywIdOptions = response.data;
        });
      }else{
        this.form.cusName = null;
        this.form.cusId = null;
        this.form.ywId=null;
        this.form.ywId2 = null;
        this.form.skMoney = 0;
        this.form.yhMoney = 0;
        this.form.ysMoney = 0;
        this.ywIdOptions =[];
      }
    },
    handleGetOrder(value){
      if(value==""||value==null){
        this.form.cusName = null;
        this.form.cusId = null;
        this.form.ywId2 = null;
        this.form.skMoney = 0;
        this.form.yhMoney = 0;
        this.form.ysMoney = 0;
      }else {
        let obj = {};
        obj = this.ywIdOptions.find((item)=>{
          return item.value === value;
        });
        this.form.skMoney = 0;
        this.form.yhMoney = 0;
        this.form.ywId2 = obj.orderId;
        this.form.cusName = obj.cusName;
        this.form.cusId = obj.cusId!=null ?parseInt(obj.cusId):null;
        if(this.form.ywType=='订单出库' || this.form.ywType=='成品出库') {
          getSurplusShoukuan(value).then(response => {
            this.form.ysMoney = obj.money - response.data;
          });
        }
      }
    },
    handleCusChange(value){
      let obj = {};
      obj = this.khList.find((item)=>{
        return item.id === value;
      });
      this.form.cusName = obj.cusName;
      this.$forceUpdate()
    },
    handleMonty(){
      let ysMoney=this.form.ysMoney
      let yhMoney=this.form.yhMoney
      this.form.skMoney=ysMoney-yhMoney
      /* if(zkMoney==null || zkMoney=="" || zkMoney==undefined || yhMoney==null || yhMoney=="" || yhMoney==undefined){
         this.form.yfMoney=0
       }else {
       }*/
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加收款记账";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShoukuan(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改收款记账";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShoukuan(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShoukuan(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除收款记账编号为"' + ids + '"的数据项？').then(function() {
        return delShoukuan(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jiesuan/shoukuan/export', {
        ...this.queryParams
      }, `shoukuan_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-input-number.is-controls-right .el-input-number__increase{
    display: none;
    visibility: hidden;
    width: 10px !important;
    position: fixed;
    top:0;
  }
  ::v-deep .el-input-number.is-controls-right .el-input-number__decrease{
    display: none;
    visibility: hidden;
    position: fixed;
    top:0;
  }
  ::v-deep .el-table .cell{
    /*display: none;*/
    padding-right: 0;
    padding-left: -30px;
  }
</style>
